
var authorization = myStorage.getItem('memberidToken');
console.log(authorization);

var goodsId = GetQueryString('goodsId');
var goodsinfoParam = {
	goodsId:goodsId
}

ltNetwork.tokenGet(configUrl.GOODSINFO_URL,goodsinfoParam,authorization,function(res){
	console.log(res);
	var data = res.data;
	
	var headPics = data.headPics;
	$('#bannerTmpl').tmpl(headPics).appendTo('#bannerSlide');
	var mySwiper = new Swiper ('.swiper-container', {
	    direction: 'horizontal',
	    loop: true,
	    autoplay:'3000',
	    autoplayDisableOnInteraction : false,	//用户操作后不会停止
	    // 如果需要分页器
	    pagination: '.swiper-pagination',
	    paginationType : 'fraction',
	 });
	 
	 $('#goodsDetailTmpl').tmpl(data).appendTo('#goodsDetail');
	 
	 var categorys = data.categorys;	//规格
	 for (var i=0; i<categorys.length; i++) {
	 	var properties = categorys[i].properties;
	 	$('.specList').eq(i).find('li').eq(0).addClass('active');
	 }
	 /*
	 * 选择规格
	 */
	var categorys = data.categorys;		//规格列表
	var specName=[],categoryName=[];
	categoryName[0] = categorys[0].categoryName;
	specName[0] = categorys[0].properties[0].propertyName;
	$('.specList').each(function(){
		var idx = $(this).index();
		console.log(categorys[idx]);
		categoryName[idx] = $(this).find('.category-name').text();
		$(this).find('li').on('click',function(){
			$(this).addClass('active').siblings().removeClass('active');
			specName[idx] = $(this).text();		//对应选择的规格
		});
	})
//	$('.specList').on('click','li',function(){
//		if($(this).hasClass('active')){
//			
//		}else{
//			$(this).addClass('active').siblings().removeClass('active');
//			$(this).append('<em></em>').siblings().find('em').remove();
//		}
//		
//	});
	
	var activityMaxNum = data.activityMaxNum-data.activitySaleNum;	//活动商品总数-已售数量
	/*
	 * 选择数量
	 */
	var productNum = (function(){
		
		var minus = function(numValue){
			
			if(numValue<=1){
				numValue =1;
			}else{
				numValue --;
			}
			$('.numIpt').val(numValue);
		};
		var addnum = function(numValue){
			
			if(numValue>=1000){
				numValue =1000;
			}else{
				numValue ++;
			}
			$('.numIpt').val(numValue);
		}
		return{
			minus:minus,
			addnum:addnum
		}
	})();
	$('.minusIpt').on('click',function(){	//数量减少
		var numValue = $('.numIpt').val();
		productNum.minus(numValue);
	});
	$('.addIpt').on('click',function(){		//数量增加
		var numValue = $('.numIpt').val();
		productNum.addnum(numValue);
	});
	
	console.log(data.collectionStatus);
	
	/*
	 * 收藏
	 */
	if(data.collectionStatus==1){	//已收藏
		$('.proTitle em').addClass('collected');
		$('.footer-collected').addClass('active');
	}else{							//未收藏
		$('.proTitle em').removeClass('collected');
		$('.footer-collected').removeClass('active');
	}
	
	//微信端页面：微信打开就已是登录
		
		$('.proTitle em').on('click',function(){
			if($(this).hasClass('collected')){	//已收藏状态，取消收藏
				$(this).removeClass('collected');
				$('.footer-collected').removeClass('active');
				addCollect(goodsId,0);
			}else{
				$(this).addClass('collected');
				$('.footer-collected').addClass('active');
				addCollect(goodsId,1);
			}
			
		});
		$('.footer-collected').on('click',function(){
			if($(this).hasClass('active')){	//已收藏状态，取消收藏
				$(this).removeClass('active');
				$('.proTitle em').removeClass('collected');
				addCollect(goodsId,0);
			}else{
				$(this).addClass('active');
				$('.proTitle em').addClass('collected');
				addCollect(goodsId,1);
			}
		});
	
	
	$('.discussTab').on('click','li',function(){
		$(this).addClass('active').siblings().removeClass('active');
		var idx = $(this).index();
		$('.discussbox').eq(idx).removeClass('dn').siblings('.discussbox').addClass('dn');
	});
	 
	//查询商品评论
	ltNetwork.get(BASE_URL+'goodsComment/list/'+goodsId+'/'+1+'/'+10,'',function(res){
		console.log(res);
		var content = res.data.content;
		$('#discussTmpl').tmpl(content,{
			starNum:function(){
				var starLevel = this.data.starLevel;
				var starArr = new Array(starLevel);
				return starArr;
			}
		}).appendTo('#discussList');
	},function(msg){
		console.log(msg);
	});

	
	var addrObj = {
		goodsId:goodsId,
		goodsNum:$('.numIpt').val(),
		categoryName:categoryName,
		specName:specName
	}
	$('.proBuy').on('click',function(){
		$(this).attr('href','cartOrderlist.html?addrObj='+JSON.stringify(addrObj));		//去支付  链接跳转
	});
	
},function(msg){
	console.log(msg);
});


function addCollect(goodsId,status){
	var collectParam = {
		status:status
	}
	ltNetwork.tokenPost(BASE_URL+'goods/collect/'+goodsId,collectParam,authorization,function(res){
		console.log(res);
	},function(msg){
		console.log(msg);
	});
}


//获取底部导航条的高度，赋值给div作为占位
var footHeight = $('.footerBox').height()+20;
$('#footerHeight').height(footHeight);